package com.carlyang.studentinfo.web;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carlyang.studentinfo.dao.ClassDao;
import com.carlyang.studentinfo.dao.StudentDao;
import com.carlyang.studentinfo.model.Class;
import com.carlyang.studentinfo.model.PageBean;
import com.carlyang.studentinfo.model.Student;
import com.carlyang.studentinfo.util.DBUtil;
import com.carlyang.studentinfo.util.JsonUtil;
import com.carlyang.studentinfo.util.LogUtil;
import com.carlyang.studentinfo.util.ResponseUtil;
import com.carlyang.studentinfo.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StudentListServlet extends HttpServlet{
	public static final String TAG = "StudentListServlet";
	StudentDao studentDao = new StudentDao();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String stuNo = request.getParameter("stuNo");
		String stuName = request.getParameter("stuName");
		String sex = request.getParameter("sex");
		String bbirthday = request.getParameter("bbirthday");
		String ebirthday = request.getParameter("ebirthday");
		String classId = request.getParameter("classId");
		LogUtil.i(TAG,"查询条件："+"学号："+stuNo+",姓名："+stuName+",性别:"+sex+",日期b："+bbirthday+",日期e："+ebirthday+",班级ID："+classId);
		
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		Student student = new Student();
		if (stuNo != null) {
			student.setStuNo(stuNo);
			student.setStuName(stuName);
			student.setSex(sex);
			if (StringUtil.isNotEmpty(classId)) {				
				student.setClassId(Integer.parseInt(classId));
			}
		}
		
		LogUtil.i(TAG, "页面请求：page:"+page+",rows:"+rows);
		PageBean pageBean = new PageBean(page,rows);
		Connection connection=null;
		try {
			LogUtil.i(TAG, "连接数据库......");
			connection = DBUtil.getConnection();
			JSONArray array = JsonUtil.formatRsToJsonArray(studentDao.getStudentList(connection, pageBean,student,bbirthday,ebirthday));
			int total = studentDao.getStudentCount(connection,student,bbirthday,ebirthday);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("rows", array);
			jsonObject.put("total", total);
			ResponseUtil.write(response, jsonObject);
			//System.out.println("json:"+jsonObject.toString());
			LogUtil.i(TAG, "json:"+jsonObject.toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeCon(connection);
				LogUtil.i(TAG, "关闭数据库连接成功！---------------------------------");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
